﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using DevExpress.ExpressApp;
using DevExpress.ExpressApp.Security.Strategy;
using DevExpress.Persistent.Base;
using DevExpress.Persistent.BaseImpl;
using DevExpress.Persistent.Validation;
using DevExpress.Xpo;

namespace L4Alive.Module.BusinessObjects
{
    [System.ComponentModel.DisplayName("Class/table Alive")]
    [ImageName("Action_SingleChoiceAction")]
    [NavigationItem("Default")]
    [DefaultProperty("ID_L4_UTIL_APP_RUNNING")]
    public class L4_UTIL_APP_RUNNING : XPBaseObject
    {
        #region Atributy
        int fID_L4_UTIL_APP_RUNNING;
        [Key(true)]
        [RuleRequiredField("RuleRequired for L4_UTIL_APP_RUNNING.ID_L4_UTIL_APP_RUNNING", DefaultContexts.Save)]
        [RuleUniqueValue("RuleUnique for L4_UTIL_APP_RUNNING.ID_L4_UTIL_APP_RUNNING", DefaultContexts.Save)]
        [DevExpress.Xpo.DisplayName("ID Class/table Alive")]
        public int ID_L4_UTIL_APP_RUNNING
        {
            get { return fID_L4_UTIL_APP_RUNNING; }
            set { SetPropertyValue<int>("ID_L4_UTIL_APP_RUNNING", ref fID_L4_UTIL_APP_RUNNING, value); }
        }

        DateTime fDT_VYTVORENI = DateTime.Now;
        [RuleRequiredField("RuleRequired for L4_UTIL_APP_RUNNING.DT_VYTVORENI", DefaultContexts.Save)]
        [DevExpress.Xpo.DisplayName("Created")]
        public DateTime DT_VYTVORENI
        {
            get { return fDT_VYTVORENI; }
            set { SetPropertyValue<DateTime>("DT_VYTVORENI", ref fDT_VYTVORENI, value); }
        }

        bool fJE_PRIHLASEN = true;
        [DevExpress.Xpo.DisplayName("Is running")]
        public bool JE_PRIHLASEN
        {
            get { return fJE_PRIHLASEN; }
            set { SetPropertyValue<bool>("JE_PRIHLASEN", ref fJE_PRIHLASEN, value); }
        }

        DateTime fDT_APP_RUNNING = DateTime.Now;
        [DevExpress.Xpo.DisplayName("App run")]
        public DateTime DT_APP_RUNNING
        {
            get { return fDT_APP_RUNNING; }
            set { SetPropertyValue<DateTime>("DT_APP_RUNNING", ref fDT_APP_RUNNING, value); }
        }

        DateTime fDT_ODHLASENI;
        [DevExpress.Xpo.DisplayName("App exit")]
        public DateTime DT_ODHLASENI
        {
            get { return fDT_ODHLASENI; }
            set { SetPropertyValue<DateTime>("DT_ODHLASENI", ref fDT_ODHLASENI, value); }
        }

        SecuritySystemUser fID_L4_User;
        [DevExpress.Xpo.DisplayName("User")]
        public SecuritySystemUser ID_L4_User
        {
            get { return fID_L4_User; }
            set { SetPropertyValue<SecuritySystemUser>("ID_L4_User", ref fID_L4_User, value); }
        }

        string fLOGIN_OS;
        [Size(200)]
        [DevExpress.Xpo.DisplayName("Login OS")]
        public string LOGIN_OS
        {
            get { return fLOGIN_OS; }
            set { SetPropertyValue<string>("LOGIN_OS", ref fLOGIN_OS, value); }
        }

        string fPOCITAC;
        [Size(200)]
        [DevExpress.Xpo.DisplayName("Computer")]
        public string POCITAC
        {
            get { return fPOCITAC; }
            set { SetPropertyValue<string>("POCITAC", ref fPOCITAC, value); }
        }

        string fPOCITAC_IP;
        [Size(50)]
        [DevExpress.Xpo.DisplayName("IP")]
        public string POCITAC_IP
        {
            get { return fPOCITAC_IP; }
            set { SetPropertyValue<string>("POCITAC_IP", ref fPOCITAC_IP, value); }
        }

        string fPOZNAMKA;
        [Size(1000)]
        [DevExpress.Xpo.DisplayName("Note")]
        public string POZNAMKA
        {
            get { return fPOZNAMKA; }
            set { SetPropertyValue<string>("POZNAMKA", ref fPOZNAMKA, value); }
        }

        int fINTERVAL;
        [Index(-1)]
        [VisibleInDetailView(false)]
        [VisibleInListView(false)]
        [VisibleInLookupListView(false)]
        public int INTERVAL
        {
            get { return fINTERVAL; }
            set { SetPropertyValue<int>("INTERVAL", ref fINTERVAL, value); }
        }

        #endregion

        #region N-vazba
        #endregion

        #region X-sloupce

        [DevExpress.Xpo.DisplayName("Is Alive")]
        public bool XZIJE
        {
            get
            {
                if (this.DT_ODHLASENI != DateTime.MinValue)
                    return false;

                DateTime DTmrtvy = DateTime.Now.AddMilliseconds((-1) * this.INTERVAL + 10000);
                if (this.DT_APP_RUNNING > DTmrtvy)
                    return true;
                else
                    return false;
            }
        }
        #endregion

        #region Metody/Funkce
        public L4_UTIL_APP_RUNNING(Session session)
            : base(session)
        {
            // This constructor is used when an object is loaded from a persistent storage.
            // Do not place any code here or place it only when the IsLoading property is false:
            // if (!IsLoading){
            //    It is now OK to place your initialization code here.
            // }
            // or as an alternative, move your initialization code into the AfterConstruction method.
        }
        public override void AfterConstruction()
        {
            base.AfterConstruction();
            // Place here your initialization code.

            //this.ID_L4_User = this.Session.GetObjectByKey<SecuritySystemUser>(this.Session.GetKeyValue(SecuritySystem.CurrentUser));
            this.POCITAC = System.Environment.MachineName;
            this.DT_VYTVORENI = DateTime.Now;
            this.DT_APP_RUNNING = DateTime.Now;

            int iMinutes = 30000;

            //if (System.Configuration.ConfigurationManager.AppSettings["DELAY_APP_RUNING"] == null ||
            //      System.Configuration.ConfigurationManager.AppSettings["DELAY_APP_RUNING"].ToString() == "")
            //    iMinutes = 600000;
            //else
            //    iMinutes = Convert.ToInt32(System.Configuration.ConfigurationManager.AppSettings["DELAY_APP_RUNING"]) * 60000;
            this.INTERVAL = iMinutes;
        }


        #endregion
    }

}
